import { _decorator, Component, Node, ColliderComponent, ITriggerEvent, ICollisionEvent, Material, LabelComponent } from "cc";
import { numberLabel } from "./numberLabel";
import { BallSystem } from "../system/ballSystem";
const { ccclass, property } = _decorator;

@ccclass("ballUnit")
export class ballUnit extends Component {
    Num: number = 2;
    LabelNode: Node = null;

    onLoad() {

    }

    public start() {
        //球号初始化
        this.initBallNumber()

        //注册碰撞事件监听
        this.getComponent(ColliderComponent).on('onCollisionEnter', this.onCollisionEnter, this);
        this.getComponent(ColliderComponent).on('onCollisionStay', this.onCollisionStay, this);
        this.getComponent(ColliderComponent).on('onCollisionExit', this.onCollisionExit, this);
        //
    }

    // update (deltaTime: number) {
    //     // Your update function goes here.
    // }
    private onTrigger(event: ITriggerEvent) {
        // console.log(event)
    }
    private onCollisionEnter(event: ICollisionEvent) {
        // console.log(event)

        if (!event.otherCollider.node.getComponent(ballUnit)) {
            return
        }

        if (this.Num == event.otherCollider.node.getComponent(ballUnit).Num) {
            event.otherCollider.node.getComponent(ballUnit).LabelNode.destroy()
            event.otherCollider.node.destroy()

            this.scheduleOnce(() => {

                this.LabelNode.getComponent(LabelComponent).string = this.Num * 2 + '';
                this.Num *= 2

            }, 0.3)

        }
    }
    private onCollisionStay(event: ICollisionEvent) {
        // console.log(event)
    }
    private onCollisionExit(event: ICollisionEvent) {
        // console.log(event)
    }


    public initBallNumber(ballNumber: number = 2) {      //若无注明参数，默认1号球
        console.log('initBallNumber')

        // this.node.getChildByName('numberLabel').parent =  cc.find('numLebelLayout')
    }



}
